package com.ruoyi.order.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.employee.domain.EmployeeUser;
import com.ruoyi.employee.mapper.EmployeeUserMapper;
import com.ruoyi.employee.service.IEmployeeUserService;
import com.ruoyi.order.domain.OrderComment;
import com.ruoyi.order.domain.OrderGoods;
import com.ruoyi.order.mapper.OrderCommentMapper;
import com.ruoyi.order.mapper.OrderGoodsMapper;
import com.ruoyi.order.service.IOrderCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 评价订单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class OrderCommentServiceImpl implements IOrderCommentService
{
    @Autowired
    private OrderCommentMapper orderCommentMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Autowired
    private IEmployeeUserService employeeUserService;

    @Autowired
    private EmployeeUserMapper employeeUserMapper;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询评价订单列表
     * 
     * @param orderComment 评价订单信息
     * @return 评价订单信息集合信息
     */
    @Override
    @DataScope(orderAlias = "o", userAlias = "u")
    public List<OrderComment> selectOrderCommentList(OrderComment orderComment)
    {
        return orderCommentMapper.selectOrderCommentList(orderComment);
    }

    /**
     * 通过评价订单编号查询评价订单
     * 
     * @param orderNo 评价订单编号
     * @return 评价订单对象信息
     */
    @Override
    public OrderComment selectOrderCommentByOrderNo(String orderNo)
    {
        return orderCommentMapper.selectOrderCommentByOrderNo(orderNo);
    }

    /**
     * 新增保存评价订单信息
     * 
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrderComment(OrderComment orderComment)
    {
        // 设置订单已评价
        OrderGoods orderGoods = orderGoodsMapper.selectOrderGoodsByOrderNo(orderComment.getOrderNo());
        orderGoods.setIsRate(UserConstants.YES);
        orderComment.setCustomerUserId(orderGoods.getOrderUserId());
        orderComment.setEmployeeUserId(orderGoods.getEmployeeUserId());
        orderGoodsMapper.updateOrderGoods(orderGoods);

        // 店员分值变化
        EmployeeUser employeeUser = employeeUserMapper.selectUserById(orderGoods.getEmployeeUserId());
        employeeUser.setScoreTotal(employeeUser.getScoreTotal() + orderComment.getCommentRate());
        double result = (employeeUser.getScore() + orderComment.getCommentRate())/2;
        double score = Math.round(result * 10)/10;
        employeeUser.setScore(score);

        employeeUserMapper.updateUser(employeeUser);

        int rows = orderCommentMapper.insertOrderComment(orderComment);
        return rows;
    }

    /**
     * 修改保存评价订单信息
     * 
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrderComment(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 修改评价订单状态
     * 
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    public int updateOrderCommentStatus(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 修改评价订单评价状态
     *
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    public int updateOrderCommentIsRate(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 修改评价订单支付状态
     *
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    public int updateOrderCommentPayStatus(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 修改评价订单接单状态
     *
     * @param orderComment 评价订单信息
     * @return 结果
     */
    @Override
    public int updateOrderCommentIsAccept(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 通过评价订单编号删除评价订单
     * 
     * @param commentId 评价订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderCommentByCommentId(Long commentId)
    {
        return orderCommentMapper.deleteOrderCommentByCommentId(commentId);
    }

    /**
     * 批量删除评价订单信息
     * 
     * @param commentIds 需要删除的评价订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderCommentByCommentIds(Long[] commentIds)
    {
        return orderCommentMapper.deleteOrderCommentByCommentIds(commentIds);
    }
}
